package com.sgb.kjwl.view.widget.transport;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.SheetDialogCarInfoBinding;
import com.sgb.kjwl.model.entity.CarInfo;
import com.sgb.kjwl.model.entity.self.CarInfoListEntity;
import com.sgb.kjwl.utils.NumberUtil;
import com.sgb.kjwl.view.ui.adapter.transport.CarInfoAdapter;
import com.swgk.core.dialog.BaseBottomDialog;
import com.swgk.core.dialog.DialogViewHolder;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.text.StrBuilder;

/**
 * Describe:
 * Author:  yangLei
 * Date:    2022/1/13
 */
public class CarListBottomDialog extends BaseBottomDialog {

    private SheetDialogCarInfoBinding mBinding;
    private Context mContext;
    private CarInfoAdapter mAdapter;

    private List<CarInfo> data;

    public CarListBottomDialog(Context context,List<CarInfo> data) {
        super(context, null);
        mContext = context;
        if (data == null) {
            this.data = new ArrayList<>();
        } else
            this.data = data;
        initList();
    }

    private void initList() {
        mBinding.rvCarsInfo.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new CarInfoAdapter(data);
        mBinding.rvCarsInfo.setAdapter(mAdapter);
    }

    @Override
    protected int dialogLayout() {
        return R.layout.sheet_dialog_car_info;
    }

    @Override
    protected void initView(DialogViewHolder holder, Bundle savedInstanceState) {
        mBinding = (SheetDialogCarInfoBinding) holder.getmBinding();
        setCanceledOnTouchOutside(false);
        mBinding.close.setOnClickListener(v -> dismiss());
    }

    class CarInfoAdapter extends BaseQuickAdapter<CarInfo, BaseViewHolder> {
        public CarInfoAdapter(@Nullable List<CarInfo> data) {
            super(R.layout.item_car_info, data);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, CarInfo item) {
            holder.setText(R.id.license_plate_number, !TextUtils.isEmpty(item.getCarNumber()) ? item.getCarNumber() : "");

            StrBuilder function = new StrBuilder("");
            if (!TextUtils.isEmpty(item.getCarTypeName())) {
                function.append(item.getCarTypeName() + "/");
            }
            if (!TextUtils.isEmpty(item.getCarLenName())) {
                function.append("车长" + item.getCarLenName() + "/");
            }
            if (item.getWeight() > 0) {
                function.append(NumberUtil.trim0(item.getWeight()) + "吨");
            }
            holder.setText(R.id.car_info, function.toString());

            String address = item.getProvinceName() + item.getCityName() + item.getDistrictName();
            holder.setText(R.id.address, address);
        }
    }

}
